package com.yilian.io;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;

/**
 * Student类 - 用于IO流练习
 * 
 * 这个类实现了Serializable接口，用于演示：
 * 1. 对象序列化和反序列化
 * 2. 文件读写操作
 * 3. 数据流处理
 * 4. 各种IO流的使用
 * 
 * @author 练习项目
 * @version 1.0
 */
public class Student implements Serializable {
    
    // 序列化版本号，用于版本兼容性
    private static final long serialVersionUID = 1L;
    
    // ==================== 基本信息 ====================
    
    /**
     * 学号
     */
    private String studentId;
    
    /**
     * 姓名
     */
    private String name;
    
    /**
     * 年龄
     */
    private int age;
    
    /**
     * 专业
     */
    private String major;
    
    /**
     * 入学日期
     */
    private LocalDate enrollmentDate;
    
    /**
     * 成绩
     */
    private double gpa;
    
    // ==================== 构造器 ====================
    
    /**
     * 默认构造器
     */
    public Student() {
        this.studentId = "未知学号";
        this.name = "未知姓名";
        this.age = 0;
        this.major = "未知专业";
        this.enrollmentDate = LocalDate.now();
        this.gpa = 0.0;
    }
    
    /**
     * 带参数构造器
     * 
     * @param studentId 学号
     * @param name 姓名
     * @param age 年龄
     * @param major 专业
     * @param enrollmentDate 入学日期
     * @param gpa 成绩
     */
    public Student(String studentId, String name, int age, String major, LocalDate enrollmentDate, double gpa) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.major = major;
        this.enrollmentDate = enrollmentDate;
        this.gpa = gpa;
    }
    
    // ==================== getter和setter方法 ====================
    
    public String getStudentId() {
        return studentId;
    }
    
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getMajor() {
        return major;
    }
    
    public void setMajor(String major) {
        this.major = major;
    }
    
    public LocalDate getEnrollmentDate() {
        return enrollmentDate;
    }
    
    public void setEnrollmentDate(LocalDate enrollmentDate) {
        this.enrollmentDate = enrollmentDate;
    }
    
    public double getGpa() {
        return gpa;
    }
    
    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
    
    // ==================== 业务方法 ====================
    
    /**
     * 获取学生信息摘要
     * 
     * @return 学生信息摘要字符串
     */
    public String getSummary() {
        return String.format("学号: %s, 姓名: %s, 专业: %s, GPA: %.2f", 
                           studentId, name, major, gpa);
    }
    
    /**
     * 判断是否为优秀学生（GPA >= 3.5）
     * 
     * @return 是否为优秀学生
     */
    public boolean isExcellentStudent() {
        return gpa >= 3.5;
    }
    
    /**
     * 获取年级信息（根据入学日期计算）
     * 
     * @return 年级字符串
     */
    public String getGrade() {
        int currentYear = LocalDate.now().getYear();
        int enrollmentYear = enrollmentDate.getYear();
        int grade = currentYear - enrollmentYear + 1;
        
        if (grade <= 0) return "新生";
        if (grade > 4) return "毕业生";
        
        return grade + "年级";
    }
    
    // ==================== 重写方法 ====================
    
    @Override
    public String toString() {
        return String.format("Student{studentId='%s', name='%s', age=%d, major='%s', enrollmentDate=%s, gpa=%.2f}",
                           studentId, name, age, major, enrollmentDate, gpa);
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        
        Student student = (Student) obj;
        return Objects.equals(studentId, student.studentId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(studentId);
    }
} 